package cn.bocweb.spinning.view.adapter;

import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ItemBean;
import cn.bocweb.spinning.util.DeviceUtil;

/**
 * Created by chenchangxing on 16/8/19.
 */
public class ItemAdapter extends BaseAdapter {

    List<ItemBean> list;
    int selection = 1000;
    OnItemClickListener onItemClickListener;

    public ItemAdapter(List<ItemBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        DeviceUtil.Log("" + list.size());
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_set_ques_warehouse, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.quesName.setText(list.get(position).getTitle());

        if (selection == position) {
            holder.btnWarehouse.setCardBackgroundColor(parent.getContext().getResources().getColor(R.color.colorPrimary));
        } else {
            holder.btnWarehouse.setCardBackgroundColor(parent.getContext().getResources().getColor(R.color.bg_set_ques));
        }

        holder.bgWarehouse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.ques_name)
        TextView quesName;
        @Bind(R.id.bg_warehouse)
        LinearLayout bgWarehouse;
        @Bind(R.id.btn_warehouse)
        CardView btnWarehouse;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setSelection(int selection) {
        this.selection = selection;
    }
}
